home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / j109lxa4.tar / ax25dump.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  5KB  |  283 lines

  1. /* AX25 header tracing
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4.  /* Mods by PA0GRI */
  5. #include <stdio.h>
  6. #include "global.h"
  7. #include "config.h"
  8. #ifdef AX25
  9. #include "mbuf.h"
  10. #include "ax25.h"
  11. #include "lapb.h"
  12. #include "trace.h"
  13. #include "socket.h"
  14.  
  15. static char *decode_type __ARGS((int16 type));
  16.  
  17. /* Dump an AX.25 packet header */
  18. void
  19. #ifdef MONITOR
  20. ax25_dump(fp,bpp,check,mon)
  21. int mon;
  22. #else
  23. ax25_dump(fp,bpp,check)
  24. #endif
  25. FILE *fp;
  26. struct mbuf **bpp;
  27. int check;    /* Not used */
  28. {
  29.     char tmp[AXBUF];
  30.     char frmr[3];
  31.     int control,pid,seg;
  32.     int16 type;
  33.     int unsegmented;
  34.     struct ax25 hdr;
  35.     char *hp;
  36.  
  37. #ifdef MONITOR
  38.     if (!mon)
  39. #endif
  40.     fprintf(fp,"AX25: ");
  41.     /* Extract the address header */
  42.     if(ntohax25(&hdr,bpp) < 0){
  43.         /* Something wrong with the header */
  44.         fprintf(fp," bad header!\n");
  45.         return;
  46.     }
  47.     fprintf(fp,"%s",pax25(tmp,hdr.source));
  48.     fprintf(fp,"->%s",pax25(tmp,hdr.dest));
  49.     if(hdr.ndigis > 0){
  50. #ifdef MONITOR
  51.         if (!mon)
  52. #endif
  53.         fprintf(fp," v");
  54.         for(hp = hdr.digis[0]; hp < &hdr.digis[hdr.ndigis][0];
  55.          hp += AXALEN){
  56.             /* Print digi string */
  57.             fprintf(fp," %s%s",pax25(tmp,hp),
  58.              (hp[ALEN] & REPEATED) ? "*":"");
  59.         }
  60.     }
  61.     if((control = PULLCHAR(bpp)) == -1)
  62.         return;
  63.  
  64.     type = ftype(control);
  65. #ifdef MONITOR
  66.     if (!mon || (type != I && type != UI))
  67.     {
  68. #endif
  69.     fprintf(fp," %s",decode_type(type));
  70.     /* Dump poll/final bit */
  71.     if(control & PF){
  72. #ifdef MONITOR
  73.         if (mon)
  74.             fprintf(fp, "!");
  75.         else
  76. #endif
  77.         switch(hdr.cmdrsp){
  78.         case LAPB_COMMAND:
  79.             fprintf(fp,"(P)");
  80.             break;
  81.         case LAPB_RESPONSE:
  82.             fprintf(fp,"(F)");
  83.             break;
  84.         default:
  85.             fprintf(fp,"(P/F)");
  86.             break;
  87.         }
  88.     }
  89. #ifdef MONITOR
  90.     }
  91. #endif
  92.     /* Dump sequence numbers */
  93. #ifdef MONITOR
  94.     if (!mon)
  95. #endif
  96.     if((type & 0x3) != U)    /* I or S frame? */
  97.         fprintf(fp," NR=%d",(control>>5)&7);
  98.     if(type == I || type == UI){    
  99. #ifdef MONITOR
  100.         if (!mon)
  101. #endif
  102.         if(type == I)
  103.             fprintf(fp," NS=%d",(control>>1)&7);
  104.         /* Decode I field */
  105.         if((pid = PULLCHAR(bpp)) != -1){    /* Get pid */
  106.             if(pid == PID_SEGMENT){
  107.                 unsegmented = 0;
  108.                 seg = PULLCHAR(bpp);
  109. #ifdef MONITOR
  110.                 if (!mon)
  111. #endif
  112.                 fprintf(fp,"%s remain %u",seg & SEG_FIRST ?
  113.                  " First seg;" : "",seg & SEG_REM);
  114.                 if(seg & SEG_FIRST)
  115.                     pid = PULLCHAR(bpp);
  116.             } else
  117.                 unsegmented = 1;
  118.  
  119.             switch(pid){
  120.             case PID_SEGMENT:
  121.                 fprintf(fp,"\n");
  122.                 break;    /* Already displayed */
  123.             case PID_ARP:
  124. #ifdef MONITOR
  125.                 if (mon)
  126.                     fprintf(fp, " ARP ");
  127.                 else
  128. #endif
  129.                 fprintf(fp," pid=ARP\n");
  130. #ifdef MONITOR
  131.                 arp_dump(fp,bpp,mon);
  132. #else
  133.                 arp_dump(fp,bpp);
  134. #endif
  135.                 break;
  136.             case PID_RARP:
  137. #ifdef MONITOR
  138.                 if (mon)
  139.                     fprintf(fp, " RARP ");
  140.                 else
  141. #endif
  142.                 fprintf(fp," pid=RARP\n");
  143. #ifdef MONITOR
  144.                 arp_dump(fp,bpp,mon);
  145. #else
  146.                 arp_dump(fp,bpp);
  147. #endif
  148.                 break;
  149.             case PID_NETROM:
  150. #ifdef MONITOR
  151.                 if (mon)
  152.                     fprintf(fp, " ");
  153.                 else
  154. #endif
  155.                 fprintf(fp," pid=NET/ROM\n");
  156.                 /* Don't verify checksums unless unsegmented */
  157. #ifdef MONITOR
  158.                 netrom_dump(fp,bpp,unsegmented,mon);
  159. #else
  160.                 netrom_dump(fp,bpp,unsegmented);
  161. #endif
  162.                 break;
  163.             case PID_IP:
  164. #ifdef MONITOR
  165.                 if (mon)
  166.                     fprintf(fp, " IP ");
  167.                 else
  168. #endif
  169.                 fprintf(fp," pid=IP\n");
  170.                 /* Don't verify checksums unless unsegmented */
  171. #ifdef MONITOR
  172.                 ip_dump(fp,bpp,unsegmented,mon);
  173. #else
  174.                 ip_dump(fp,bpp,unsegmented);
  175. #endif
  176.                 break;
  177.             case PID_X25:
  178. #ifdef MONITOR
  179.                 if (mon)
  180.                     fprintf(fp, " X25\n");
  181.                 else
  182. #endif
  183.                 fprintf(fp," pid=X.25\n");
  184.                 break;
  185.             case PID_TEXNET:
  186. #ifdef MONITOR
  187.                 if (mon)
  188.                     fprintf(fp, " TEXNET\n");
  189.                 else
  190. #endif
  191.                 fprintf(fp," pid=TEXNET\n");
  192.                 break;
  193.             case PID_NO_L3:
  194. #ifdef MONITOR
  195.                 if (mon)
  196.                     fprintf(fp, "\n");
  197.                 else
  198. #endif
  199.                 fprintf(fp," pid=Text\n");
  200.                 break;
  201.             default:
  202. #ifdef MONITOR
  203.                 if (mon)
  204.                     fprintf(fp, " [0x%x]\n", pid);
  205.                 else
  206. #endif
  207.                 fprintf(fp," pid=0x%x\n",pid);
  208.             }
  209.         }
  210.     } else if(type == FRMR && pullup(bpp,frmr,3) == 3){
  211.         fprintf(fp,": %s",decode_type(ftype(frmr[0])));
  212.         fprintf(fp," Vr = %d Vs = %d",(frmr[1] >> 5) & MMASK,
  213.             (frmr[1] >> 1) & MMASK);
  214.         if(frmr[2] & W)
  215.             fprintf(fp," Invalid control field");
  216.         if(frmr[2] & X)
  217.             fprintf(fp," Illegal I-field");
  218.         if(frmr[2] & Y)
  219.             fprintf(fp," Too-long I-field");
  220.         if(frmr[2] & Z)
  221.             fprintf(fp," Invalid seq number");
  222.         fprintf(fp,"\n");
  223.     } else
  224.         fprintf(fp,"\n");
  225.  
  226. }
  227. static char *
  228. decode_type(type)
  229. int16 type;
  230. {
  231.     switch(type){
  232.     case I:
  233.         return "I";
  234.     case SABM:
  235.         return "SABM";
  236.     case DISC:
  237.         return "DISC";
  238.     case DM:
  239.         return "DM";
  240.     case UA:
  241.         return "UA";
  242.     case RR:
  243.         return "RR";
  244.     case RNR:
  245.         return "RNR";
  246.     case REJ:
  247.         return "REJ";
  248.     case FRMR:
  249.         return "FRMR";
  250.     case UI:
  251.         return "UI";
  252.     default:
  253.         return "[invalid]";
  254.     }
  255. }
  256.  
  257. /* Return 1 if this packet is directed to us, 0 otherwise. Note that
  258.  * this checks only the ultimate destination, not the digipeater field
  259.  */
  260. int
  261. ax_forus(iface,bp)
  262. struct iface *iface;
  263. struct mbuf *bp;
  264. {
  265.     struct mbuf *bpp;
  266.     char dest[AXALEN];
  267.  
  268.     /* Duplicate the destination address */
  269.     if(dup_p(&bpp,bp,0,AXALEN) != AXALEN){
  270.         free_p(bpp);
  271.         return 0;
  272.     }
  273.     if(pullup(&bpp,dest,AXALEN) < AXALEN)
  274.         return 0;
  275.     if(addreq(dest,iface->hwaddr))
  276.         return 1;
  277.     else
  278.         return 0;
  279. }
  280.  
  281. #endif /* AX25 */
  282.  
  283.